package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.util.MyUtil;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by 528787067@qq.com on 2017/7/4.
 */

public class DepartmentRemaneActivity extends SlidingActivity implements View.OnClickListener {

	@ViewInject(R.id.position_edit_tv)
	private EditText editText;

	private Intent intent;
	private String organizaId;
	private String departmentName;
	private String departmentId;
	private int type;

	@Override
	protected int getResourceId() {
		return R.layout.activity_position;
	}

	@Override
	protected void onStart() {
		super.onStart();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				MyUtil.startKeyword(mContext, editText);
			}
		}, 300);
	}

	@Override
	protected void init() {
		intent = getIntent();
		departmentId = intent.getStringExtra("departmentId");
		departmentName = intent.getStringExtra("departmentName");
		organizaId = intent.getStringExtra("organizaId");
		type = intent.getIntExtra("type", 1);
		if(departmentName == null)
			departmentName = "";
		initTitle();
		initView();
	}

	private void initTitle(){
		topName.setText("修改部门名称");
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		rightIcon.setVisibility(View.GONE);
		rightBtn.setVisibility(View.VISIBLE);
		rightBtn.setText("保存");
		leftIcon.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
	}

	private void initView(){
		editText.setHint("输入部门名称");
		editText.setText(departmentName);
		editText.setSelection(departmentName.length());
	}

	@Override
	public void finish() {
		MyUtil.closeKeyword(this, editText);
		super.finish();
	}

	@Override
	public void onClick(View v) {
		if(v == leftIcon){
			onBackPressed();
		} else if(v == rightBtn){
			if(editText.getText().toString().trim().length() == 0){
				ToasUtil.toast(mContext, "请输入有效的部门名称");
			} else if(editText.getText().toString().equals(departmentName)){
				onBackPressed();
			} else{
				JsonObject param = new JsonObject();
				param.addProperty("organizaId", organizaId);
				param.addProperty("depaId", departmentId);
				param.addProperty("type", type);
				param.addProperty("name", editText.getText().toString());
				HttpHelper.getInstance(mContext).renameDepartment(param, 0x01, new ResultSubscriber.OnResultListener() {
					@Override
					public void onStart(int requestType) {
						loading.show();
					}
					@Override
					public void onCompleted(int requestType) {
						loading.cancel();
					}
					@Override
					public void onError(int requestType) {
						onCompleted(requestType);
					}
					@Override
					public void onNext(IModel data, int requestType) {
						if(data.getErrcode() != 0){
							ToasUtil.toast(mContext, data.getMessage());
							onError(requestType);
						} else{
							setResult(RESULT_OK, intent);
							onBackPressed();
						}
					}
				});
			}
		}
	}
}

